#!/bin/bash
# chkconfig: - 85 15
#/*
# * Copyright 2017 ~ 2025 the original author or authors. <Wanglsir@gmail.com, 983708408@qq.com>
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# *      http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */

[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0

# Load the user environment.
[ -f "/etc/bashrc" ] && . /etc/bashrc
[ -f "/etc/bash.bashrc" ] && . /etc/bash.bashrc # e.g ubuntu
if [ "$USER" == "root" ]; then
  . "/root/.bash_profile"
  . "/root/.bashrc"
else
  [ -f "/home/$USER/.bash_profile" ] && . /home/$USER/.bash_profile
  [ -f "/home/$USER/.bashrc" ] && . /home/$USER/.bashrc
  # Mac OS
  [ -f "/Users/$USER/.bash_profile" ] && . /Users/$USER/.bash_profile
  [ -f "/Users/$USER/.bashrc" ] && . /Users/$USER/.bashrc
fi
[ -f "/etc/profile.d/profile-kafka-manager.sh" ] && . /etc/profile.d/profile-kafka-manager.sh

# Initialize directory. (if not exists)
mkdir -p $KAFKA_MANAGER_CONF_DIR
mkdir -p $KAFKA_MANAGER_DATA_DIR
mkdir -p $KAFKA_MANAGER_LOG_DIR

function start() {
  local jvmOpts=${KAFKA_MANAGER_OPTS:-'-Dhttp.port=9000'}
  local pids=$(getPids)
  if [ -z "$pids" ]; then
    # Must remove.
    rm -rf $KAFKA_MANAGER_HOME/RUNNING_PID
    rm -rf $KAFKA_MANAGER_HOME/application.home*
    nohup $KAFKA_MANAGER_HOME/bin/kafka-manager -Dconfig.file=$KAFKA_MANAGER_CONF_DIR/application.conf ${jvmOpts} >"$KAFKA_MANAGER_LOG_DIR/kafka-manager.stdout" 2>&1 &

    echo -n "Starting kafka-manager ..."
    while true
    do
      pids=$(getPids)
      if [ "$pids" == "" ]; then
        echo -n ".";
        sleep 0.8;
      else
        echo $pids >"$KAFKA_MANAGER_DATA_DIR/kafka-manager.pid"
        break
      fi
    done
    echo -e "\nStarted kafka-manager on "$pids
  else
    echo "kafka-manager process is running "$pids
  fi
}

function stop() {
  local pids=$(getPids)
  if [ -z "$pids" ]; then
    echo "kafka-manager not running!"
  else
    echo -n "Stopping kafka-manager for $pids ..."
    kill -s TERM $pids
    while true
    do
      pids=$(getPids)
      if [ "$pids" == "" ]; then
        \rm -f $KAFKA_MANAGER_DATA_DIR/kafka-manager.pid
        break
      else
        echo -n ".";
        sleep 0.8;
      fi
    done
    echo -e "\nStopped kafka-manager !"
  fi
}

function status() {
  ps -ef | grep -v grep | grep $KAFKA_MANAGER_CONF_DIR
}

function getPids() {
  local pids=$(ps ax | grep -i "$KAFKA_MANAGER_CONF_DIR" | grep -v grep | awk '{print $1}')
  echo $pids # Output execution result value.
  return 0 # Return the execution result code.
}

# --- Main call. ---
case $1 in
  status)
    status
    ;;
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
    *|--help|help)
  echo $"Usage: {start|stop|restart|status}"
  exit 2
esac